package com.yupi.springbootinit.controller;

import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.model.vo.DeviceStatusVO;
import com.yupi.springbootinit.service.DeviceDataService;
import com.yupi.springbootinit.service.StatisticsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:WwW
 * @Description
 * @Date: 2025/9/19  09:06
 */
@RestController
@RequestMapping("/statistics")
@RequiredArgsConstructor
public class StatisticsController {

    private final StatisticsService statisticsService;
    private final DeviceDataService deviceDataService;

    /**
     * 稼动率查询
     * GET /statistics/{deviceId}/availability?start=-24h&end=now()
     */
    @GetMapping("/{deviceId}/availability")
    public BaseResponse<Double> availability(@PathVariable Integer deviceId,
                                             @RequestParam(defaultValue = "-24h") String range,
                                             @RequestParam(defaultValue = "true") boolean includeBusy) {
        double value = statisticsService.calcAvailability(deviceId, range, includeBusy);
        return ResultUtils.success(value);
    }

    /**
     * 打印进度
     * GET /statistics/{deviceId}/progress
     */
    @GetMapping("/{deviceId}/progress")
    public BaseResponse<Double> progress(@PathVariable Integer deviceId) {
        DeviceStatusVO status = deviceDataService.getLatestStatus(deviceId);
        double progress = statisticsService.calcProgress(status);
        return ResultUtils.success(progress);
    }
}
